define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teacher_detail/index' + location.search,
                    add_url: 'teacher_detail/add' + location.search,
                    edit_url: 'teacher_detail/edit' + location.search,
                    del_url: 'teacher_detail/del',
                    multi_url: 'teacher_detail/multi',
                    import_url: 'teacher_detail/import',
                    table: 'teacher_detail',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'info_id', title: __('Info_id')},
                        {
                            field: 'club.name',
                            title: __('Club_one'),
                            operate: 'LIKE',
                            halign: "center",
                            searchable: false,
                            formatter: function(value, row, index) {
                                return value || '';
                            }
                        },
                        {
                            field: 'club_dtl.name',
                            title: __('Club_two'),
                            operate: 'LIKE',
                            halign: "center",
                            searchable: false,
                            formatter: function(value, row, index) {
                                return value || '';
                            }
                        },
                        {field: 'is_all', title: __('Is_all'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},

                        {field: 'hourly_rate', title: __('Hourly_rate'), operate:'BETWEEN'},
                        {field: 'is_premium', title: __('是否精品'), formatter: Table.api.formatter.toggle, searchList: {0: __('否'), 1: __('是')}},
                        // {field: 'score', title: __('Score'), operate: 'BETWEEN', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user_score', title: __('User_score'), operate: 'BETWEEN', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'people', title: __('People'), operate: 'BETWEEN', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'detail_status', title: __('上架'), formatter: Table.api.formatter.toggle, searchList: {2: __('否'), 1: __('是')}},
                        {field: 'addtime', title: __('Addtime'), operate:false, addclass:'datetimerange', autocomplete:false},
                        // {field: 'word_file', title: __('Word_file'), operate: false, formatter: Table.api.formatter.file},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                
                // 设置子社团下拉的动态参数，根据主社团过滤
                $("#c-club_two_id").data("params", function () {
                    var clubOneId = $("#c-club_one_id").val();
                    if (clubOneId) {
                        return {custom: {club_id: clubOneId}};
                    }
                    return {};
                });
                
                // 监听主社团变化，清空子社团选择并重新加载
                $("#c-club_one_id").on('change', function() {
                    $("#c-club_two_id").selectPageClear();
                    // 重新设置参数
                    var clubOneId = $(this).val();
                    if (clubOneId) {
                        $("#c-club_two_id").data("params", function () {
                            return {custom: {club_id: clubOneId}};
                        });
                    }
                });
            }
        }
    };
    return Controller;
});
